package orchestration.android;

import orchestration.persistence.OrchestratorAuthority.AndroidProcessExecution;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
/**
 * 
 * @author <a href="mailto:car.ferrarini@gmail.com">Carlo Ferrarini</a>
 *
 * An Activity that "runs" processes: 
 * - if no executionUri was specified, it creates a new execution for the given process.
 * - it starts a new RunExecutionActivity to run the execution, spreading the given input
 * - when the RunExecutionActivity ends, the output is spread to the caller. The output will contain the new execution id so that if the sub-execution was paused
 * it will be possible to resume it later
 * 
 * input: executionUri: String (send null to create a new Instance)
 * output: executionUri: String
 * 
 * At this level of abstraction, "run an execution" means run all the task
 * that are received through notifications in a linear way
 */
public class RunProcessActivity extends Activity {

	private static final String TAG="RunProcessActivity";
	
	private static final String EXECUTION_URI_KEY = "executionUri";
	private String executionUriString;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if(savedInstanceState==null){
			//launch only if activity was not destroyed!
			Uri executionUri;
			Intent intent= getIntent();
			executionUriString=intent.getStringExtra(EXECUTION_URI_KEY);
			if(executionUriString==null){
				//create the new execution
				String procId= intent.getData().getPathSegments().get(1);
				ContentValues cv= new ContentValues();
				cv.put(AndroidProcessExecution.NAME, "");
				cv.put(AndroidProcessExecution.PROCESS_REF, procId);
				executionUri=getContentResolver().insert(AndroidProcessExecution.CONTENT_URI, cv);
				executionUriString=executionUri.toString();
			}else{
				executionUri= Uri.parse(executionUriString);
			}
			//create an intent with to start the activity for the execution
			Intent runExecutionIntent=  new Intent(Intent.ACTION_RUN, executionUri);
			//fill the intent extras
			runExecutionIntent.putExtras(intent.getExtras());
			//start the activity
			int requestCode=Integer.parseInt(executionUri.getPathSegments().get(1));
			startActivityForResult(runExecutionIntent, requestCode);
		}else
			executionUriString=savedInstanceState.getString(EXECUTION_URI_KEY);
	}
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString(EXECUTION_URI_KEY, executionUriString);
	}
	/**
	 * sends the result back to the "parent" activity.
	 * delete the sub-execution if it is ended.
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		Intent intent = getIntent();
		if(resultCode==RESULT_OK){
			//delete the sub-execution
			Uri subExexutionUri= Uri.parse(executionUriString);
			getContentResolver().delete(subExexutionUri, null, null);
			//spread the result to the previous activity
			intent.putExtras(data.getExtras());
			
		}
		Log.w(TAG, "saving executionUriString=" + executionUriString);
		intent.putExtra(EXECUTION_URI_KEY, executionUriString);
		setResult(resultCode, intent);
		finish();
	}
}
